package controller

import (
	"go_web/project/dao"
	"go_web/project/model"
	"html/template"
	"net/http"
	"strconv"
)

//func IndexHandler(w http.ResponseWriter, r *http.Request) {
//	pageNo := r.FormValue("pageNo")
//	if pageNo == "" {
//		pageNo = "1"
//	}
//	page, _ := dao.GetPageBooks(pageNo)
//	// 解析模板
//	t := template.Must(template.ParseFiles("project/views/index.html"))
//	// 执行
//	t.Execute(w, page)
//}

//func GetBooks(w http.ResponseWriter, r *http.Request) {
//	books, _ := dao.GetBooks()
//	t := template.Must(template.ParseFiles("project/views/pages/manager/book_manager.html"))
//	t.Execute(w, books)
//
//}
// 带分页
func GetPageBooks(w http.ResponseWriter, r *http.Request) {
	pageNo := r.FormValue("pageNo")
	if pageNo == "" {
		pageNo = "1"
	}
	page, _ := dao.GetPageBooks(pageNo)
	t := template.Must(template.ParseFiles("project/views/pages/manager/book_manager.html"))
	t.Execute(w, page)

}

// 通过价格查找,返回的带分页
func GetPageBooksByPrice(w http.ResponseWriter, r *http.Request) {
	pageNo := r.FormValue("pageNo")
	minPrice := r.FormValue("min")
	maxPrice := r.FormValue("max")
	if pageNo == "" {
		pageNo = "1"
	}
	var page *model.Page
	if minPrice == "" && maxPrice == "" {
		page, _ = dao.GetPageBooks(pageNo)
	} else {
		page, _ = dao.GetPageBooksByPrice(pageNo, minPrice, maxPrice)
		page.MinPrice = minPrice
		page.MaxPrice = maxPrice
	}
	flag, session := dao.IsLogin(r)
	if flag {
		page.IsLogin = true
		page.Username = session.UserName
	}

	t := template.Must(template.ParseFiles("project/views/index.html"))
	t.Execute(w, page)

}
func AddBook(w http.ResponseWriter, r *http.Request) {
	title := r.PostFormValue("title")
	price := r.PostFormValue("price")
	author := r.PostFormValue("author")
	sales := r.PostFormValue("sales")
	stock := r.PostFormValue("stock")

	fPrice, _ := strconv.ParseFloat(price, 64)
	iSales, _ := strconv.ParseInt(sales, 10, 0)
	iStock, _ := strconv.ParseInt(stock, 10, 0)
	book := &model.Book{
		Title:   title,
		Price:   fPrice,
		Author:  author,
		Sales:   int(iSales),
		Stock:   int(iStock),
		ImgPath: "static/img/default.jpg",
	}

	dao.AddBook(book)
	// 添加完以后查询数据库返回前端
	GetPageBooks(w, r)
}

func DeleteBook(w http.ResponseWriter, r *http.Request) {
	bookId := r.FormValue("bookId")
	dao.DeleteBook(bookId)
	// 删除完以后查询数据库返回前端
	GetPageBooks(w, r)
}

func ToUpdateBookPage(w http.ResponseWriter, r *http.Request) {
	bookId := r.FormValue("bookId")
	book, _ := dao.GetBookById(bookId)
	// 通过book的id判断修改还是添加
	if book.ID > 0 {
		t := template.Must(template.ParseFiles("project/views/pages/manager/book_edit.html"))
		t.Execute(w, book)
	} else {
		t := template.Must(template.ParseFiles("project/views/pages/manager/book_edit.html"))
		t.Execute(w, nil)
	}

}
func UpdateOrAddBook(w http.ResponseWriter, r *http.Request) {
	bookId := r.PostFormValue("bookId")
	title := r.PostFormValue("title")
	price := r.PostFormValue("price")
	author := r.PostFormValue("author")
	sales := r.PostFormValue("sales")
	stock := r.PostFormValue("stock")

	fPrice, _ := strconv.ParseFloat(price, 64)
	iSales, _ := strconv.ParseInt(sales, 10, 0)
	iStock, _ := strconv.ParseInt(stock, 10, 0)
	iBookId, _ := strconv.ParseInt(bookId, 10, 0)
	book := &model.Book{
		ID:      int(iBookId),
		Title:   title,
		Price:   fPrice,
		Author:  author,
		Sales:   int(iSales),
		Stock:   int(iStock),
		ImgPath: "project/views/static/img/default.jpg",
	}
	if book.ID > 0 {
		dao.UpdateBook(book)
	} else {
		dao.AddBook(book)
	}
	GetPageBooks(w, r)
}
